package server.email;

import java.security.Security;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.NoSuchProviderException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMail {
	private static final Properties props = prepareConnectionProperties();
	private String to;
	private String subject;
	private String text;

	public SendMail(String to, String subject, String text) {
		this.to = to;
		this.subject = subject;
		this.text = text;
	}

	public void send() throws NoSuchProviderException, AddressException {

		try {
			Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
			Session session = Session.getDefaultInstance(props, new GJMailAuthenticator());
			session.setDebug(true);
			Transport transport = session.getTransport();
			InternetAddress addressFrom = new InternetAddress("forvao@gmail.com");
			MimeMessage message = new MimeMessage(session);
			message.setSender(addressFrom);
			message.setSubject(subject);
			message.setContent(text, "text/html");
			InternetAddress addressTo = new InternetAddress(to);
			message.setRecipient(Message.RecipientType.TO, addressTo);
			transport.connect();
			Transport.send(message);
			transport.close();
			System.out.println("DONE");

		} catch (Exception e) {

			e.printStackTrace();
		}
	}

	private static Properties prepareConnectionProperties() {
		Properties props = new Properties();
		props.setProperty("mail.transport.protocol", "smtp");
		props.setProperty("mail.host", "smtp.gmail.com");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "465");
		props.put("mail.debug", "true");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.socketFactory.fallback", "false");
		return props;
	}
}

class GJMailAuthenticator extends javax.mail.Authenticator {
	private static final String SMTP_AUTH_USER = "forvao@gmail.com";
	private static final String SMTP_AUTH_PWD = "4osixkmppid";

	protected PasswordAuthentication getPasswordAuthentication() {
		return new PasswordAuthentication(SMTP_AUTH_USER, SMTP_AUTH_PWD);

	}
}